#pragma once
#include "compiler.hpp"
#include "runner.hpp"
#include <jsoncpp/json/json.h>
#include "../comm/log.hpp"
#include "../comm/util.hpp"
#include <signal.h>
#include <unistd.h>
namespace ns_compile_and_run
{

    using namespace ns_log;
    using namespace ns_util;
    using namespace ns_compiler;
    using namespace ns_runner;
    class CompileAndRun
    {

    public:
        // 临时文件的删除
        static void RemoveTempFile(const std::string &file_name)
        {

            std::string _src = PathUtil::Src(file_name);
            if (FileUtil::IsFileExists(_src))
                unlink(_src.c_str());
            std::string _compiler_error = PathUtil::CompilerError(file_name);
            if (FileUtil::IsFileExists(_compiler_error))
                unlink(_compiler_error.c_str());
            std::string _execute = PathUtil::Exe(file_name);
            if (FileUtil::IsFileExists(_execute))
                unlink(_execute.c_str());
            std::string _stdin = PathUtil::Stdin(file_name);
            if (FileUtil::IsFileExists(_stdin))
                unlink(_stdin.c_str());
            std::string _stdout = PathUtil::Stdout(file_name);
            if (FileUtil::IsFileExists(_stdout))
                unlink(_stdout.c_str());
            std::string _stderr = PathUtil::Stderr(file_name);
            if (FileUtil::IsFileExists(_stderr))
                unlink(_stderr.c_str());
        }

    public:
        static std::string CodeToDesc(int code, const std::string &filename)
        {
            std::string desc = "";
            switch (code)
            {
            case 0:
                desc = "编译运行成功";
                break;
            case -1:
                desc = "提交的代码为空";
                break;
            case -2:
                desc = "未知错误";
                break;
            case -3:
                FileUtil::ReadFile(PathUtil::CompilerError(filename), &desc, true);
                break;
            case SIGABRT:
                desc = "内存超过范围";
                break;
            case SIGXCPU:
                desc = "CPU资源超过规定值";
            default:
                desc = "内部错误：" + std::to_string(code);
                break;
            }
            return desc;
        }
      
    public:
        static void Start(const std::string &in_json, std::string *out_json)
        {

            Json::Value in_value;
            Json::Reader reader;
            reader.parse(in_json, in_value); // 反序列化
            std::string code = in_value["code"].asString();
            std::string input = in_value["input"].asString();
            int cpu_limit = in_value["cpu_limit"].asInt();
            int mem_limit = in_value["mem_limit"].asInt();

            Json::Value out_value;
            int status_code = 0;
            std::string file_name = FileUtil::UniqFileName();
            int run_result = 0;
            if (code.size() == 0)
            {
                // 代码为空
                status_code = -1;
                goto END;
            }

            // 形成临时文件
            if (!FileUtil::WriteFile(PathUtil::Src(file_name), code))
            {
                status_code = -2;
             

                goto END;
            }

            if (!Compiler::Compile(file_name))
            {
                status_code = -3;
                std::string reason;

                FileUtil::ReadFile(PathUtil::CompilerError(file_name), &reason, true);
                out_value["reason"] = reason;
                goto END;
            }
            run_result = Runner::Run(file_name, cpu_limit, mem_limit);
            if (run_result < 0)
            {
                //(内部错误)
                status_code = -2;
            }
            else if (run_result > 0)
            {
                // 运行错误

                status_code = run_result;
            }
            else
            {
                // 运行成功
                status_code = 0;
            }
        END:
            out_value["status"] = status_code;
            out_value["reason"] = CodeToDesc(status_code, file_name);
            if (status_code == 0)
            {
                std::string _stderr;
                std::string _stdout;

                FileUtil::ReadFile(PathUtil::Stderr(file_name), &_stderr, true);
                out_value["stderr"] = _stderr;

                FileUtil::ReadFile(PathUtil::Stdout(file_name), &_stdout, true);
                out_value["stdout"] = _stdout;
            }
            Json::StyledWriter Writer;
            *out_json = Writer.write(out_value);
           RemoveTempFile(file_name);
        }
    };

}